package com.WindowFunction;


import com.MenuFunction.CreateMenu;

import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public class MoveWindowsAction {
    private static int _x;
    private static int _y;
    /**
     * 实现窗口拖拉效果  和  展示菜单的效果
     * @param frame
     */
    public  void moveWindows(JFrame frame){
        // 添加鼠标事件处理以实现拖动功能
        frame.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                // 存储鼠标按下的位置
                _x = e.getX();
                _y = e.getY();
                if(e.getButton() == MouseEvent.BUTTON3){
                    new CreateMenu().CreateRightClickMenu(frame,e);        //菜单
                }
            }
        });
        frame.addMouseMotionListener(new MouseMotionAdapter() {     //鼠标运动监听器
            @Override
            public void mouseDragged(MouseEvent e) {
                // 获取当前屏幕位置
                int x = e.getXOnScreen();
                int y = e.getYOnScreen();
                // 更新窗口位置
                frame.setLocation(x - _x, y - _y);
            }
        });
    }
}
